#!/usr/bin/env python
#
# Copyright 2011 Justin Bruce Van Horne <justinvh@gmail.com>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Abstracts a camera object for use in various elements of the game.

 Camera: The camera class for describing a camera object.
 FlatCamera: A derivative of Camera for usage in over-the-top views.
"""

from pyglet.gl import *
from pyglet.event import *


class Camera(object):
    """Abstract camera class."""
    def project(self):
        """Set up the GL projection matrix. Leave us in GL_MODELVIEW mode. """
        raise NotImplemented()

    def on_resize(self, width, height):
        """Handle resize of the viewport. """
        raise NotImplemented()


class FlatCamera(Camera):
    """A flat camera is a camera with a fixed over-the-top view."""
    def __init__(self, x, y, width, height, near=-50, far=50):
        """Initialize the camera with a certain width and height."""
        self.x, self.y = x, y
        self.z = 0
        self.width, self.height = width, height
        self.near, self.far = near, far

    def project(self):
        """Creates the actual projection."""
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glViewport(0, 0, self.width, self.height)
        glOrtho(0, self.width, 0, self.height, self.near, self.far)
        glTranslatef(self.x, self.y, self.z)
        glMatrixMode(GL_MODELVIEW)

    def on_resize(self, width, height):
        """Deal with resizes."""
        self.width, self.height = width, height
        return EVENT_UNHANDLED
